package com.imooc.course.server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.course.server.dao.FileDao;
import com.imooc.course.server.dto.FileDto;
import com.imooc.course.server.dto.PageDto;
import com.imooc.course.server.entity.File;
import com.imooc.course.server.entity.FileExample;
import com.imooc.course.server.service.FileService;
import com.imooc.course.server.utils.CopyUtil;
import com.imooc.course.server.utils.UuidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("fileService")
public class FileServiceImpl implements FileService {

    private static final Logger log = LoggerFactory.getLogger(FileServiceImpl.class);

    @Resource(name = "fileDao")
    private FileDao fileDao;

    /**
     * 列表查询
     * @param pageDto
     */
    @Override
    public void getList(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());

        FileExample fileExample = new FileExample();
        List<File> fileList = fileDao.selectByExample(fileExample);

        PageInfo<File> filePageInfo = new PageInfo<>(fileList);
        pageDto.setTotal(filePageInfo.getTotal());

        List<FileDto> fileDtoList = CopyUtil.copyList(fileList, FileDto.class);

        pageDto.setList(fileDtoList);
    }

    /**
     * 保存，id有值为更新，id无值为新增
     * @param fileDto
     */
    @Override
    public void save(FileDto fileDto) {

        File fileDb = getFileByKey(fileDto);

        if (fileDb == null) {
            File file = CopyUtil.copy(fileDto, File.class);
            this.addFile(file);
        } else {
            fileDb.setShardIndex(fileDto.getShardIndex());
            this.updateFile(fileDb);
        }
    }

    private File getFileByKey(FileDto fileDto) {
        FileExample example = new FileExample();
        example.createCriteria().andKeyEqualTo(fileDto.getKey());
        List<File> files = fileDao.selectByExample(example);
        return files.size() != 0 ? files.get(0) : null;
    }

    private void updateFile(File copy) {
        fileDao.updateByPrimaryKeySelective(copy);
    }

    private void addFile(File copy) {
        copy.setId(UuidUtil.getShortUuid());
        fileDao.insertSelective(copy);
    }

    @Override
    public FileDto getByShardIndex(String key) {
        FileDto fileDto = new FileDto();
        fileDto.setKey(key);
        File file = this.getFileByKey(fileDto);
        return CopyUtil.copy(file, FileDto.class);
    }

    @Override
    public FileDto getByKey(String key) {
        FileExample example = new FileExample();
        example.createCriteria().andKeyEqualTo(key);
        List<File> files = fileDao.selectByExample(example);
        File file = files.size() != 0 ? files.get(0) : null;
        return CopyUtil.copy(file,FileDto.class);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id) {
        fileDao.deleteByPrimaryKey(id);
    }

    /**
     * 删除
     * @param key
     */
    @Override
    public void removeByKey(String key) {
        FileExample example = new FileExample();
        example.createCriteria().andKeyEqualTo(key);
        fileDao.deleteByExample(example);
    }
}
